package cn.com.baozun.sany.workbench.application.publisher;

import cn.com.baozun.sany.workbench.application.event.OnlineChangeEvent;
import cn.com.baozun.sany.workbench.online.manager.entity.OnlineChange;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * @title: PublishService
 * @description:
 * @author: guan.xin
 * @create: 2021-05-26 20:06
 * @since: JDK8
 */
@Component
public class OnlineChangePublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    public void sendMessage() {
        OnlineChange oc = new OnlineChange();
        oc.setStaffId(new Random().nextLong());
        oc.setPreStatus("在线");
        oc.setPostStatus("休息");
        oc.setChangeTime(LocalDateTime.now());
        oc.setAuditor("张三");

        this.publish(new OnlineChangeEvent(this, oc));
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void publish(ApplicationEvent applicationEvent) {
        this.applicationEventPublisher.publishEvent(applicationEvent);
    }
}
